<?php

namespace app\common\model;

use app\admin\model\Command;
use think\Model;

/**
 * 订单类
 */
class Order extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    // 追加属性
    protected $append = [
        'createtime_text',
        'settlement_time_text',
        'jiesuan_time_text',
        'image_text',
        'call_image_text',
        'baoxiao_msg_text',
        'money_msg_text',
        'audit_upload_time_msg_text'
    ];

    public function getMoneyMsgTextAttr($value, $data)
    {
        $value = '';
        if (isset($data['baoxiao_status']) && isset($data['is_more'])) {
            if ($data['baoxiao_status'] == 1) {
                $value = '待报销';
            } elseif ($data['baoxiao_status'] == 2) {
                $value = '已报销';
            } elseif ($data['baoxiao_status'] == 3) {
                $value = '首月待报销';
            } elseif ($data['baoxiao_status'] == 4) {
                $value = '首月已报销';
            }
            if ($data['baoxiao_status'] == 5) {
                $value = '首月待报销\次月待报销';
            } elseif ($data['baoxiao_status'] == 6) {
                $value = '首月已报销\次月已报销';
            }
        }
        return $value;
    }

    public function getAuditUploadTimeMsgTextAttr($value, $data)
    {
        $value = '';
        if (isset($data['order_status']) && isset($data['audit_upload_time']) && $data['audit_upload_time'] > 0 && isset($data['is_more']) && isset($data['goods_id'])) {
            if ($data['order_status'] == 4) {
                $packages = Packages::where(['id' => $data['goods_id']])->find();
                if ($data['is_more'] == 1) {
                    $value = '';
                }

            }
        }
        return $value;
    }

    public function getImageTextAttr($value, $data)
    {
        if (!empty($data['image'])) {
            $value = cdnurl($data['image'], true, 2);
        }
        return $value;
    }

    public function getCallImageTextAttr($value, $data)
    {
        if (!empty($data['call_image'])) {
            $value = cdnurl($data['call_image'], true, 2);
        }
        return $value;
    }

    public function getBaoxiaoMsgTextAttr($value, $data)
    {
        if (isset($data['baoxiao_status']) && isset($data['order_status']) && isset($data['jiesuan_time']) && isset($data['goods_id'])) {

//            if ($data['baoxiao_status'] == 1) {
//                $xiayue    = date('m', strtotime("+1 month"));
//                $nextMonth = $xiayue + 1;
//                if ($nextMonth < 10) {
//                    $nextMonth = '0'.$nextMonth;
//                }
//
//                $jie_day = date('d', $data['jiesuan_time']);
//                if ($jie_day > 30) {
//                    $jie_day = 30;
//                }
//                $packages = Packages::where(['id' => $data['goods_id']])->find();
//                $money    = 0;
//                if ($packages) {
//                    $money = round((30 - $jie_day) * ($packages['epact_set_meal'] / 30) + ($packages['ciyue_set_meal'] * 2),
//                        2);
//                }
//                $value = $xiayue."月28日前保持在网，".$nextMonth."月10日将为您报销".$money."元";
//            }
        }
        return $value;
    }

    public static function baoxiaoMsg($data)
    {
//        if ($data['baoxiao_status'] == 1) {
            $xiayue    = date('m', strtotime("+1 month"));
            $nextMonth = $xiayue + 1;
            if ($nextMonth < 10) {
                $nextMonth = '0'.$nextMonth;
            }

            $jie_day = date('d', $data['jiesuan_time']);
            if ($jie_day > 30) {
                $jie_day = 30;
            }
            $packages = Packages::where(['id' => $data['goods_id']])->find();
            $money    = 0;
            if ($packages) {
                $money = round((30 - $jie_day) * ($packages['epact_set_meal'] / 30) + ($packages['ciyue_set_meal'] * 2),
                    2);
            }
            if($data['is_more'] == 1){
                $value = '本单将于2024-'.$xiayue.'-'.$nextMonth.'左右一次性报销话费'.$money.'元。';
            }else{
                if($data['more_num'] == 1){
                    $value = '本单将于2024-'.$xiayue.'-'.$nextMonth.'左右报销首月实付套餐费'.$money.'元。';
                }else{
                    $value = '本单将于2024-'.$xiayue.'-'.$nextMonth.'报销首月套餐费'.$money.'元。';
                }
            }
//        }
        return $value;
    }

    public function getCreatetimeTextAttr($value, $data)
    {
        if (isset($data['createtime'])) {
            $value = date('Y-m-d H:i:s', $data['createtime']);
        }
        return $value;
    }

    public function getShippingIdcardAttr($value, $data)
    {
        if (isset($data['shipping_idcard'])) {
            $value = substr_replace($data['shipping_idcard'], "**********", 4, 10);
        }
        return $value;
    }

    public function getSettlementTimeTextAttr($value, $data)
    {
        if (isset($data['settlement_time'])) {
            $value = date('Y-m-d H:i:s', $data['settlement_time']);
        }
        return $value;
    }

    public function getJiesuanTimeTextAttr($value, $data)
    {
        if (isset($data['audit_upload_time']) && isset($data['is_more']) && isset($data['order_status']) && isset($data['audit_status']) && isset($data['more_num'])) {
            if ($data['order_status'] == 4) {
                $jiesuan_time = $data['two_js_time'];
                if ($jiesuan_time && $data['audit_status'] == 2) {
                    if ($data['is_more'] == 1) {
                        $value = '本单将于'.date('Y-m-d', $jiesuan_time).'左右结算';
                    } else {
                        if ($data['more_num'] == 0) {
                            $value = '本单将于'.date('Y-m-d', $jiesuan_time).'左右进行首次结算';
                        } else {
                            $value = '本单将于'.date('Y-m-d', $jiesuan_time).'左右进行次月结算';
                        }
                    }
                } else {
                    if ($data['is_more'] == 1) {
                        $value = '首次结算：按要求首充后七日内结算';
                    } else {
                        $value = '首月结算时间：按要求首充后七日内结算';
                    }
                }
            }
        }
        return $value;
    }

    public function packages()
    {
        return $this->belongsTo('app\common\model\Packages', 'goods_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function orderCreateAdd($params, $UserInfo, $info, $orderId)
    {
        $inser['platform_type'] = $info['platform_type'];
        $inser['user_id']       = $UserInfo['id'];
        $inser['parent_id']     = $UserInfo['parent_id'];
        if (isset($params['partnerId'])) {
            $inser['partnerId'] = $params['partnerId'];
        }
        $inser['goods_id']          = $info['id'];
        $inser['shipping_contact']  = $params['certName'];        //
        $inser['shipping_phone']    = $params['contactNum'];      //
        $inser['shipping_idcard']   = $params['certNo'];          //
        $inser['shipping_province'] = $params['postProvinceName'];//
        $inser['shipping_city']     = $params['postCityName'];
        $inser['shipping_area']     = $params['postDistrictName'];
        $inser['shipping_address']  = $params['postAddr'];
        if (!empty($params['number'])) {
            $inser['phone'] = $params['number'];
        }
        if(!empty($params['outTradeNo'])){
            $inser['outTradeNo']  = $params['outTradeNo'];
        }
        $inser['package_name'] = $info['name'];
        if (!empty($params['agid'])) {
            $inser['user_rebate'] = UserPackagesConfig::where([
                    'user_id'     => $params['agid'],
                    'packages_id' => $info['id']
                ])->value('money') ?? 0;
        }
        $inser['is_more'] = 1;
        if (!empty($info['duoci'])) {
            $inser['is_more'] = 2;
        }
        $inser['order_id']        = $orderId??0;
        $inser['is_agency']       = 2;
        $inser['is_verification'] = $info['is_verification'];
        $inser['agency_area_id']  = 0;
        if(isset($inser['shipping_area']) && $inser['shipping_area'] !=''){
            $cityAgency               = db('agency')->where(['area' => $inser['shipping_area'], 'type' => 2])->find();
            if ($cityAgency) {
                $inser['agency_area_id'] = $cityAgency['user_id'];
            }
        }
        $inser['gys']  = $info['supplier'];
        $inser['plot_id'] = 0;
        if(isset($params['plot']) && $params['plot']!=''){
            $plotAgency       = db('agency')->where([
                'area'    => $inser['shipping_area'],
                'address' => $params['plot'],
                'type'    => 1
            ])->find();
            if ($plotAgency) {
                $inser['plot_id'] = $plotAgency['user_id'];
            }
        }
        $inser['post_area'] = $inser['shipping_province'].'-'.$inser['shipping_city'];
        if($inser['shipping_area']){
            $inser['post_area'] .= '-'.$inser['shipping_area'];
        }
        $r = self::create($inser);
        if ($r) {
            if ($inser['is_more'] == 2) {
                $packagesInfo          = $info;
                $order_id              = $r->id;
                $arr                   = json_decode($packagesInfo['duoci'], true);
                $inser_two['order_id'] = $order_id;
                $inser_two['goods_id'] = $packagesInfo['id'];
                $inser_two['user_id']  = $UserInfo['id'];
                $inser_two['money']    = $arr[0]['zigou'];
                OrderMore::create($inser_two);
                if ($UserInfo['parent_id'] > 0 && $arr[0]['one']) {
                    $inser_two['order_id'] = $order_id;
                    $inser_two['goods_id'] = $packagesInfo['id'];
                    $inser_two['user_id']  = $UserInfo['parent_id'];
                    $inser_two['money']    = $arr[0]['one'];
                    $inser_two['level']    = 2;
                    OrderMore::create($inser_two);
                }
                if ($UserInfo['two_id'] > 0 && $arr[0]['two']) {
                    $inser_two['order_id'] = $order_id;
                    $inser_two['goods_id'] = $packagesInfo['id'];
                    $inser_two['user_id']  = $UserInfo['two_id'];
                    $inser_two['money']    = $arr[0]['two'];
                    $inser_two['level']    = 3;
                    OrderMore::create($inser_two);
                }

                if ($inser['agency_area_id'] && $arr[0]['area']) {
                    $inser_two['order_id'] = $order_id;
                    $inser_two['goods_id'] = $packagesInfo['id'];
                    $inser_two['user_id']  = $inser['agency_area_id'];
                    $inser_two['money']    = $arr[0]['area'];
                    $inser_two['level']    = 4;
                    OrderMore::create($inser_two);
                }

                if ($inser['plot_id'] && $arr[0]['xiaoqu']) {
                    $inser_two['order_id'] = $order_id;
                    $inser_two['goods_id'] = $packagesInfo['id'];
                    $inser_two['user_id']  = $inser['plot_id'];
                    $inser_two['money']    = $arr[0]['xiaoqu'];
                    $inser_two['level']    = 5;
                    OrderMore::create($inser_two);
                }
            }
        }
    }

    public function getRebateRecoverMoneyAttr($value,$data)
    {
        if(isset($data['rebate_recover_msg']) && isset($data['user_id']) && isset($data['id'])){
            $value = MoneyLog::where(['order_id'=>$data['id'],'memo'=>$data['rebate_recover_msg'],'user_id'=>$data['user_id']])->value('money');
            $value =abs($value);
        }
        return $value;
    }
}

